<!-- MSD等级 -->
<template>
  <basic-container>
    <avue-crud
      v-model="form"
      ref="crud"
      :option="option"
      :table-loading="loading"
      :defaults.sync="defaults"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      :before-close="beforeClose"
      @row-save="rowSave"
      @row-update="addUpdate"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuRight">
        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>
      </template>
      <!-- 删除 -->
      <template slot="menuLeft">
        <el-button type="danger" size="small" icon="el-icon-delete" plain v-if="permission.msd_grade_delete" @click="handleDelete">删 除</el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import { getList, getLevelsList, remove, add } from '@/api/product/msd/grade';
import { mapGetters } from 'vuex';
import userTableConfig from '@/components/user-table-config/index.vue';
import userConfig from '@/mixins/userConfig';
export default {
  components: {
    userTableConfig
  },
  mixins: [userConfig()],
  data() {
    return {
      form: {},
      query: {},
      defaults: {},
      loading: true,
      data: [],
      commonDialog: {
        dispatch: true
      },
      tableOption: {}, //公用模态框配置信息
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'calc(100vh - 440px)',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 5,
        border: true,
        index: true,
        indexLabel: '序号',
        addBtn: true,
        viewBtn: true,
        editBtn: true,
        delBtn: true,
        selection: true,
        dialogClickModal: false,
        menu: true,
        labelWidth: 150,
        columnBtn: false,
        column: [
          {
            label: 'MSD等级',
            prop: 'levels',
            search: true,
            type: 'select',
            multiple: true,
            tags: true,
            dicData: [],
            hide: true,
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false
          },
          {
            label: 'MSD等级',
            prop: 'msdLevel'
          },
          {
            label: '标准使用寿命（H）',
            type: 'number',
            minRows: 0,
            prop: 'msdLifetime'
          },
          {
            label: '状态',
            prop: 'status',
            type: 'switch',
            dataType: 'number',
            dicData: [
              { label: '禁用', value: 0 },
              { label: '启用', value: 1 }
            ],
            value: 1,
            // searchValue: 1,
            search: true
          },
          {
            label: '备注',
            prop: 'remark',
            type: 'textarea',
            span: 24,
            maxRows: 3,
            minRows: 3,
            // maxlength: 300,
            overHidden: true,
            showWordLimit: true
          },
          {
            label: '更新人',
            prop: 'updateName',
            addDisplay: false,
            editDisplay: false
          },
          {
            label: '更新时间',
            prop: 'updateTime',
            addDisplay: false,
            editDisplay: false
          }
        ]
      },
      modalName: '', //打开公用模态框名称
      tableOption: {}, //公用模态框配置信息
      timer: new Date().getTime() //不同内容使用同一个模态框结构，key值不同需要重新请求渲染数据，否则会请求到上一个使用的数据内容
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
    getLevelsList()
      .then((res) => {
        let data = res.data.data;
        let findIndex = this.option.column.findIndex((item) => item.prop == 'levels');
        this.option.column[findIndex].dicData = data.map((ele) => {
          return {
            label: ele,
            value: ele
          };
        });
      })
      .catch((err) => {});
  },
  computed: {
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(',');
    },
    ...mapGetters(['permission']),
    permissionList() {
      return {
        delBtn: this.vaildData(this.permission.msd_grade_delete, false)
      };
    }
  },

  methods: {
    rowSave(row, done, loading) {
      add(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          done();
        },
        (error) => {
          loading();
          console.log(error);
        }
      );
    },
    addUpdate(row, index, done, loading) {
      add(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          done();
        },
        (error) => {
          loading();
          console.log(error);
        }
      );
    },
    rowDel(row) {
      this.$confirm(`确认删除MSD等级${row.msdLevel}?`, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请选择至少一条数据');
        return;
      }
      let msg = this.selectionList.map((item) => item.msdLevel).join(',');
      this.$confirm(`确认删除MSD等级${msg}?`, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      done();
    },
    beforeClose(done, type) {
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },

    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      if (params.levels) {
        params.levels = params.levels.join(',');
      }
      console.log('params', params);
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.idc-record-text {
  color: #409eff;
  cursor: pointer;
}
</style>
